package com.bz.xtcx.manager.mapper;

import com.bz.xtcx.manager.entity.CmsCategory;
import com.bz.xtcx.manager.mapper.provider.CmsCategoryProvider;
import com.bz.xtcx.manager.persist.conditions.CategoryCondition;
import com.bz.xtcx.manager.vo.VoQueryCms;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.StatementType;

import java.util.List;

public interface CmsCategoryMapper {
    @Select("select id, name, code, parent_id, type_id, icon, only_url, content_url, " +
            "sort, readable, tags, memo, creater, create_time, updater, update_time " +
            "from cms_category where code = #{code,jdbcType=VARCHAR}")
    @Results(
            id = "cmsCategoryRow",
            value = {
                    @Result(property = "id", column = "id"),
                    @Result(property = "name", column = "name"),
                    @Result(property = "code", column = "code"),
                    @Result(property = "parentId", column = "parent_id"),
                    @Result(property = "typeId", column = "type_id"),
                    @Result(property = "icon", column = "icon"),
                    @Result(property = "onlyUrl", column = "only_url"),
                    @Result(property = "contentUrl", column = "content_url"),
                    @Result(property = "sort", column = "sort"),
                    @Result(property = "readable", column = "readable"),
                    @Result(property = "tags", column = "tags"),
                    @Result(property = "memo", column = "memo"),
                    @Result(property = "creater", column = "creater"),
                    @Result(property = "createTime", column = "create_time"),
                    @Result(property = "updater", column = "updater"),
                    @Result(property = "updateTime", column = "update_time"),
            }
    )
    CmsCategory selectByCode(String code);

    @Select("select count(DISTINCT a.id) from cms_category a, cms_category b where a.parent_id = b.id and b.code = #{code,jdbcType=VARCHAR}")
    int countByParentCode(String code);

    @SelectProvider(type = CmsCategoryProvider.class, method = "selectByParentCodeProvider")
    @ResultMap(value = "cmsCategoryRow")
    List<CmsCategory> selectByParentCode(CategoryCondition cc);
    
    @SelectProvider(type = CmsCategoryProvider.class, method = "findByCondition")
    @ResultMap("cmsCategoryRow")
    List<CmsCategory> findByCondition(VoQueryCms query);
    
    @Insert("insert into `cms_category`(id, name, code, parent_id, type_id, icon, only_url, content_url, tags, sort, readable, memo, creater, updater) "
    		+ "values(#{id, jdbcType=VARCHAR}, #{name, jdbcType=VARCHAR}, #{code, jdbcType=VARCHAR},"
    		+ " #{parentId, jdbcType=VARCHAR}, #{typeId, jdbcType=INTEGER}, #{icon, jdbcType=VARCHAR},"
    		+ " #{onlyUrl, jdbcType=INTEGER}, #{contentUrl, jdbcType=VARCHAR}, #{tags, jdbcType=VARCHAR},"
    		+ " #{sort, jdbcType=INTEGER}, #{readable, jdbcType=INTEGER}, #{memo, jdbcType=VARCHAR},"
    		+ " #{creater, jdbcType=VARCHAR}, #{updater, jdbcType=VARCHAR}"
    		+ ") ")
    @SelectKey(before = true, keyProperty = "id", resultType = String.class, statementType = StatementType.STATEMENT, statement="select uuid()")
    int insert(CmsCategory e);
    
    @Delete("delete from `cms_category` where id = #{id}")
    int del(String id);

    @Update("update `cms_category` set name=#{name, jdbcType=VARCHAR},"
    		+ " code=#{code, jdbcType=VARCHAR}, parent_id=#{parentId, jdbcType=VARCHAR},"
    		+ " type_id=#{typeId, jdbcType=INTEGER}, icon=#{icon, jdbcType=VARCHAR},"
    		+ " only_url=#{onlyUrl, jdbcType=INTEGER}, content_url=#{contentUrl, jdbcType=VARCHAR},"
    		+ " tags=#{tags, jdbcType=VARCHAR}, sort=#{sort, jdbcType=INTEGER},"
    		+ " readable=#{readable, jdbcType=INTEGER}, memo=#{memo, jdbcType=VARCHAR},"
    		+ " updater=#{updater, jdbcType=VARCHAR}"
    		+ " where id = #{id}")
    int update(CmsCategory e);
    
    @Update("update `cms_category` set readable = #{status} where id = #{id}")
    int updateStatus(@Param("status")int status, @Param("id")String id);
    
    @Select("select * from `cms_category` where name = #{name} and ifnull(parent_id,'')=''")
    @ResultMap("cmsCategoryRow")
    CmsCategory findParByName(String name);
    
    @Select("select * from `cms_category` where code = #{code}")
    @ResultMap("cmsCategoryRow")
    CmsCategory findByCode(String code);
    
    @Select("select * from `cms_category` where id = #{id}")
    @ResultMap("cmsCategoryRow")
    CmsCategory findById(String id);
    
    @Select("select * from `cms_category` where parent_id = #{id}")
    @ResultMap("cmsCategoryRow")
    List<CmsCategory> findByParentId(String id);
    
    @Select("select * from cms_category where id not in (select ifnull(parent_id,'') from cms_category )")
    @ResultMap("cmsCategoryRow")
    List<CmsCategory> findLeafNodes();
    
    @Select("select * from `cms_category` where parent_id = #{id} and name = #{name}")
    @ResultMap("cmsCategoryRow")
    CmsCategory findByParentIdAndName(@Param("id")String id, @Param("name")String name);
}